package boot

import (
	"gfrest/common/util"
	"os"

	"github.com/gogf/gcache-adapter/adapter"
	"github.com/gogf/swagger"

	"github.com/gogf/gf/frame/g"
)

func init() {
	ENV := os.Getenv("ENV")
	GOTESTS_CONFIG_PATH := os.Getenv("GOTESTS_CONFIG_PATH")
	println("ENV", ENV)
	println("GOTESTS_CONFIG_PATH", GOTESTS_CONFIG_PATH)

	if GOTESTS_CONFIG_PATH == "" {
		g.Cfg().SetPath("./config")
	} else {
		g.Cfg().SetPath(GOTESTS_CONFIG_PATH)
	}
	if ENV == "pro" {
		g.Cfg().SetFileName("pro_config.yaml")
	} else if ENV == "test" {
		g.Cfg().SetFileName("test_config.yaml")
	} else {
		g.Cfg().SetFileName("config.yaml")
	}
	g.Server().SetPort(g.Cfg().GetInt("httpPort", 8199))

	s := g.Server()

	s.EnablePProf() // pprof; disable if not need

	// s.SetIndexFolder(false)
	// s.SetServerRoot("./static")

	if g.Cfg().GetBool("DEBUG") {
		g.Server().Plugin(&swagger.Swagger{})
	}

	// https://goframe.org/database/gdb/model/cache
	// 查询缓存
	adapter1 := adapter.NewRedis(g.Redis())
	g.DB().GetCache().SetAdapter(adapter1)

	adapter2 := adapter.NewRedis(g.Redis())
	util.Cache.SetAdapter(adapter2)
}
